/*
 * @Author: zulezhe
 * @Date: 2022-03-25 16:37:44
 * @LastEditors: zulezhe
 * @LastEditTime: 2022-03-29 15:30:44
 * @Path: https://gitee.com/zulezhe/
 * @Description:
 */
const path = require("path");
const webpack = require("webpack");
const CompressionWebpackPlugin = require("compression-webpack-plugin"); //gzip压缩
const WebpackBar = require("webpackbar");
let resolve = (dir) => {
  return path.resolve(__dirname, dir);
};
console.log(
  `==========当前运行环境:${process.env}=========${process.env.VUE_APP_SourceMap}`
);
module.exports = {
  //基本路径
  publicPath: "./",
  productionSourceMap: Boolean(process.env.VUE_APP_SourceMap),
  devServer: {
    host: "0.0.0.0",
    port: 3030, // 端口号
    https: false, // https:{type:Boolean}
    open: true, //配置自动启动浏览器
    hotOnly: true, // 热更新
    proxy: {
      [process.env.VUE_APP_API_PREFIX]: {
        target: process.env.VUE_APP_API_URL, // 目标接口域名
        changeOrigin: true, // 是否跨域
        logLevel: "debug",
      },
    },
  },
  pluginOptions: {
    "style-resources-loader": {
      preProcessor: "less",
      patterns: [
        path.resolve(__dirname, "src/assets/styles/variables.less"),
        path.resolve(__dirname, "src/assets/styles/mixin.less"),
      ],
    },
  },
  configureWebpack: (config) => {
    config.devtool = "#eval-source-map";
    config.plugins.push(
      new WebpackBar({
        name: `\u6570\u6167\u0041\u0049\u0042\u0075\u0069\u006c\u0064\u0065\u0072`,
        color: "#f40",
      })
    );
    // 忽略打包配置
    if (process.env === "production") {
      config.plugins.push(
        new CompressionWebpackPlugin({
          test: new RegExp("\\.(" + productionGzipExtensions.join("|") + ")$"),
          threshold: 10240, // 对超过10k的数据压缩
          deleteOriginalAssets: false, // 不删除源文件
          minRatio: 0.8,
        })
      );
    }
  },
  chainWebpack: (config) => {
    config.plugins.delete("preload"); // TODO: need test
    config.plugins.delete("prefetch"); // TODO: need test
    // set svg-sprite-loader
    config.module
      .rule("svg")
      .exclude.add(resolve("src/assets/icons/svg"))
      .end();
    config.module
      .rule("icons")
      .test(/\.svg$/)
      .include.add(resolve("src/assets/icons/svg"))
      .end()
      .use("svg-sprite-loader")
      .loader("svg-sprite-loader")
      .options({
        symbolId: "icon-[name]",
      })
      .end();
    //   config.module
    //     .rule("vue")
    //     .test(/\.vue$/)
    //     .use("style-vw-loader")
    //     .loader("style-vw-loader");
  },
};
